package com.etc.service.impl;

import com.etc.dao.RecordDao;
import com.etc.dao.RecorddetailsDao;
import com.etc.entity.Record;
import com.etc.entity.Recorddetails;
import com.etc.service.RecorddetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.RollbackException;
import java.util.List;

/**
 * Created by Administrator on 2019/5/21.
 */
@Service
public class RecorddetailsServiceImpl implements RecorddetailsService {
    @Autowired
    private RecorddetailsDao recorddetailsDao;

    @Autowired
    private RecordDao recordDao;

    @Transactional(propagation = Propagation.REQUIRED, readOnly = false)
    public int reTran(Record record, String[] seatid) {
        int rid;
        try {
            Record r = recordDao.save(record);
            rid = r.getRid();
            if (r != null) {
                for (int i = 0; i < seatid.length; i++) {
                    Recorddetails rd = new Recorddetails();
                    rd.setRid(r.getRid());
                    rd.setSeatid(seatid[i]);
                    recorddetailsDao.save(rd);
                }

            }
        } catch (RollbackException e) {
            return 1;
        }
        return rid;
    }

    @Override
    public List<String> findSeatidByCid(Integer cid) {
        List<String> seats = recorddetailsDao.findSeatByCid(cid);//得到已选座位的个数
        return seats;
    }
}
